import Events from "events";

const mineEvents = new Events();

const EventsEmit = (key: string, ...args: any[]) => {
  mineEvents.emit(key, ...args);
};

const useEvents = <T>() => {
  let fn: (data: T) => void;
  const EventsOn = (key: string, fun: (data: T) => void) => {
    fn = fun;
    mineEvents.on(key, fun);
  };

  const EventsOff = (key: string) => {
    if (!fn) {
      console.error("关闭监听必须先监听");
    }
    mineEvents.off(key, fn);
  };

  return { EventsOn, EventsOff };
};

export { EventsEmit, useEvents };
